<template>

  <div class="product-add">
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" label-width="200px">
      <!--店员修改-->
      <div class="common-form">修改</div>
     
      <el-form-item label="小区名称" prop="name" :rules="[{required: true,message: ' '}]">
        <el-input class="max-w460" v-model="form.name" placeholder="请输入小区名称"></el-input>
      </el-form-item>

      <el-form-item label="所属区域">
        <el-select v-model="form.province_id" placeholder="省" @change="initCity">
          <el-option :label="item.name" :value="item.id" v-for="(item,index) in areaList" :key='index'></el-option>
        </el-select>
        <el-select v-if="form.province_id!=''" v-model="form.city_id" placeholder="市" @change="initRegion">
          <el-option :label="item1.name" :value="item1.id" v-for="(item1,index1) in areaList[form.province_id]['city']"
            :key='index1'></el-option>
        </el-select>
        <el-select v-if="form.city_id!=''" v-model="form.region_id" placeholder="区">
          <el-option :label="item2.name" :value="item2.id" v-for="(item2,index2) in areaList[form.province_id]['city'][form.city_id]['region']"
            :key='index2'></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="详细地址" prop="address" :rules="[{required: true,message: ' '}]">
        <el-input class="max-w460" v-model="form.address" placeholder="请输入详细地址"></el-input>
      </el-form-item>

      <el-form-item label="小区坐标">
        <el-row class="mb16">
          <el-col :span="24">
            <el-input v-model="form.coordinate" class="max-w460"></el-input>
          </el-col>
        </el-row>
        <el-row class="mb16">
          <el-col :span="24">
            <Getpoint :form="form" @getMapdata="getMapdataFunc" @chose="choseFunc"></Getpoint>
          </el-col>
        </el-row>

      </el-form-item>

      <el-form-item label="状态">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button size="small" type="info" @click="cancelFunc">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" :loading="loading">提交</el-button>
      </div>
    </el-form>

    <!--添加-->
    <GetUser :is_open="open_add" @close="closeDialogFunc($event, 'add')"></GetUser>

  </div>
</template>

<script>
import villageApi from '@/api/village.js';
import GetUser from '@/components/user/GetUser.vue';
import {formatModel} from '@/utils/base.js'
import Getpoint from '@/components/map/Getpoint.vue';
import DataApi from '@/api/data.js';

export default {
  components: {
    /*编辑组件*/
    GetUser,
    Getpoint
  },
  data() {
    return {
      /*切换菜单*/
      // activeIndex: '1',
      /*form表单数据*/
      form: {
        status: 1,
        name: '',
        address: '',
        province_id: '',
        city_id: '',
        region_id: '',
        coordinate:'',
    
      },
      villageList:[],
      areaList:[],
      /*是否打开添加弹窗*/
      open_add: false,
 
      loading: false,
    };
  },
  created() {

    this.form.id = this.$route.query.id;
    this.getParams()
    this.getData();
   

  },

  methods: {
    getData(){
        let self = this;
        DataApi.getRegion({}, true)
          .then(res => {
            self.areaList = res.data.regionData;
          })
          .catch(error => {

          });
      },

    getParams() {
      let self = this;
      // 取到路由带过来的参数

      villageApi.villageDetail({
        id: self.form.id
      }, true)
        .then(data => {
          // 将数据放在当前组件的数据内
          self.form = formatModel(self.form, data.data.model);
          self.form.status = data.data.model.status.value;
      
        })
        .catch(error => {

        });

    },

  

    onSubmit() {
      let self = this;
      let form = self.form;
      self.$refs.form.validate((valid) => {
        if (valid) {
          self.loading = true;
          villageApi.editvillage(form, true)
            .then(data => {
              self.$message({
                message: '修改成功',
                type: 'success'
              });
              self.$router.push('/village/village/index');
            })
            .catch(error => {
              self.loading = true;
            });
        }
      });
    },

    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    },

    
    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    },

     /*初始化城市id*/
      initCity() {
        this.form.city_id = ''
      },

      /*初始化区id*/
      initRegion() {
        this.form.region_id = ''
      },

      /*获取经纬度*/
      getMapdataFunc(e) {
        this.form.coordinate = e.data[0].toFixed(6) + ',' + e.data[1].toFixed(6);
      },

      /*选择的地址*/
      choseFunc(e){
        this.form.coordinate=e.location.lat+','+e.location.lng;
        this.form.address=e.address;

      },

    

  }

};
</script>

<style lang="scss" scoped>
.basic-setting-content {
}

.product-add {
  padding-bottom: 50px;
}
</style>
